<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl as tImgUrl;

class ActivityPage extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'activity_page';


    //状态
    const ACTIVITY_PAGE_STATUS_GROUNDING = 1; //上架
    const ACTIVITY_PAGE_STATUS_UNDERCARRIAGE = 2;//下架


    public static function create($data)
    {
        $data['status'] = self::ACTIVITY_PAGE_STATUS_UNDERCARRIAGE;
        $data['addtime'] = date('Y-m-d H:i:s');

        $ret = self::insert($data);

        if ($ret->rowCount()) {
            return self::id();
        }
        return false;
    }

    public static function upData($activityId,$data)
    {
        $ret=self::update($data,['activity_id'=>$activityId]);
        if($ret->rowCount()){
            return true;
        }
        return false;
    }

    public static function getList($where=[],$limit=[],$order=['addtime'=>'DESC'],$fields='*')
    {
        $where['ORDER']=$order;

        if(!empty($limit)){
            $where['LIMIT']=$limit;
        }

        $list=self::select($fields,$where);

        if(empty($list)){
            return [];
        }
        return $list;
    }

    public static function getCount($where=[])
    {
        $list=self::select(['activity_id'],$where);

        if (empty($list)){
            return 0;
        }
        return count($list);
    }

    public static function getInfo($activityId)
    {
        $info=self::get('*',['activity_id'=>$activityId]);
        self::format($info);
        return $info;
    }

    private static function format(&$info)
    {
        foreach($info as $key=> $value){
            switch ($key){
                case 'share_img':
                    if(!empty($value)){
                        $info['share_img_url']=tImgUrl::getUrl(tImgUrl::APPTYPE_BIZ, $value, tImgUrl::IMAGE_SIZE_A_PIC);
                    }
                    break;
            }
        }
    }


}